package com.xuecheng.content.controller;

import com.xuecheng.commons.constants.CourseConstants;
import com.xuecheng.commons.model.dto.CourseAuditDto;
import com.xuecheng.commons.model.dto.CourseBaseDto;
import com.xuecheng.commons.model.dto.CourseDto;
import com.xuecheng.commons.model.dto.PageRequest;
import com.xuecheng.commons.model.vo.CourseBaseVo;
import com.xuecheng.commons.model.vo.ResponseResult;
import com.xuecheng.commons.model.vo.WorkCountVo;
import com.xuecheng.commons.utils.BeanHelper;
import com.xuecheng.content.config.MinioTemplate;
import com.xuecheng.content.domain.CourseBase;
import io.minio.MinioClient;
import org.springframework.web.bind.annotation.*;
import com.xuecheng.content.service.CourseBaseService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.multipart.MultipartFile;

import java.util.Map;

/**
 * <p>
 * 课程基本信息前端控制器
 * </p>
 *
 * @author itheima
 */
@Slf4j
@RestController
public class CourseBaseController {

    @Autowired
    private CourseBaseService  courseBaseService;

    @Autowired
    private MinioTemplate minioTemplate;//文件上传

    //分页、条件查询
    @PostMapping("/course/list")
    public ResponseResult<CourseBaseVo> findByPage(@RequestBody CourseDto courseDto, PageRequest pageRequest){
        return courseBaseService.findByPage(courseDto,pageRequest);
    }

    //课程图片上传
    @PostMapping("/course/upload")
    public ResponseResult<String> uploadPic(MultipartFile uploadFile) throws Exception{
        //上传文件
        String picUrl= minioTemplate.upload(uploadFile.getInputStream(), uploadFile.getOriginalFilename());
        return ResponseResult.okResult(picUrl);
    }

    //课程新增
    @PostMapping("/course")
    public ResponseResult<Map> saveCourseBase(@RequestBody CourseBaseDto courseBaseDto){
        return courseBaseService.saveCourseBase(courseBaseDto);
    }

    //根据courseId查询课程
    @GetMapping("/course/{courseBaseId}")
    public ResponseResult<CourseBaseVo> findById(@PathVariable("courseBaseId")Long courseBaseId){
        //service查询
        CourseBase courseBase = courseBaseService.getById(courseBaseId);
        //数据库实体转vo
        CourseBaseVo courseBaseVo = BeanHelper.copyProperties(courseBase, CourseBaseVo.class);
        //返回vo
        return ResponseResult.okResult(courseBaseVo);
    }

    //课程修改
    @PutMapping("/course")
    public ResponseResult<Map> updateCourseBase(@RequestBody CourseBaseDto courseBaseDto){
        return courseBaseService.updateCourseBase(courseBaseDto);
    }

    //课程删除
    @DeleteMapping("/course/{courseId}")
    public ResponseResult deleteCourseBase(@PathVariable("courseId")Long courseId){
        return courseBaseService.deleteCourseBase(courseId);
    }

    //课程提交审核
    @GetMapping("/course/commit/{courseBaseId}")
    public ResponseResult commitCourseBase(@PathVariable("courseBaseId")Long courseBaseId){
        //更新状态 已提交
        courseBaseService.lambdaUpdate().set(CourseBase::getAuditStatus, CourseConstants.AUDIT_COMMIT_STATUS)
                .eq(CourseBase::getId,courseBaseId).update();
        //返回200
        return ResponseResult.okResult();
    }

    //课程审核
    @PostMapping("/course/approve")
    public ResponseResult approve(@RequestBody CourseAuditDto courseAuditDto){
        //更新状态 设置审核意见
        courseBaseService.lambdaUpdate().set(CourseBase::getAuditStatus,courseAuditDto.getAuditStatus())
                .set(CourseBase::getAuditMind,courseAuditDto.getAuditMind())
                .eq(CourseBase::getId,courseAuditDto.getCourseBaseId()).update();
        //返回200
        return ResponseResult.okResult();
    }

    //课程下架
    @GetMapping("/course/offline/{courseId}")
    public ResponseResult offline(@PathVariable("courseId")Long courseId){
        return courseBaseService.offline(courseId);
    }


}
